package com.jsmtr.rms.utils;

import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public  class DateTools {

    public static String toLenStr(Date date) {
        if (date == null) {
            return null;
        }
        String result = null;
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            result = format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String toShortStr(Date date) {
        if (date == null) {
            return null;
        }
        String result = null;
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            result = format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String toDayStr(Date date) {
        if (date == null) {
            return null;
        }
        String result = null;
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            result = format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static Date toDate(String date) {
        if (date == null) {
            return null;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static Date toDate(Date date) {
        if (date == null) {
            return null;
        }
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.parse(format.format(date));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date toLenDate(String date) {
        if (date == null) {
            return null;
        }
        try {
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date getFirstDate(Date date) {
        if (date == null) {
            return null;
        }
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

            String _date = cal.get(Calendar.YEAR) + "-01-01";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.parse(_date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date getLastDate(Date date) {
        if (date == null) {
            return null;
        }
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            String _date = cal.get(Calendar.YEAR) + "-12-31";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.parse(_date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date AddDay(Date date, int day) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.DAY_OF_MONTH, day);
            return cal.getTime();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int BirthdayToAge(Date birthday) {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthday)) {
            return 0;
        }

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

        cal.setTime(birthday);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                // monthNow==monthBirth
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                // monthNow>monthBirth
                age--;
            }
        }
        return age;
    }
}